package cn.xfdzx.android.apps.shop.util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import com.cmic.sso.sdk.AuthRegisterViewConfig;
import com.cmic.sso.sdk.utils.rglistener.CustomInterface;
import com.geetest.onelogin.OneLoginHelper;
import com.geetest.onelogin.config.OneLoginThemeConfig;
import com.geetest.onelogin.listener.AbstractOneLoginListener;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.MobileLoginActivity;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.evenbus.IMloginMessage;
import cn.xfdzx.android.apps.shop.evenbus.MyInfoRefreshMessage;
import cn.xfdzx.android.apps.shop.evenbus.WeChatLoginEvent;
import cn.xfdzx.android.apps.shop.widget.WeChatLoginInterface;
import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;
import static cn.xfdzx.android.apps.shop.apps.Constants.ONELOGIN_DIALOG_MODE;
import static cn.xfdzx.android.apps.shop.apps.Constants.ONELOGIN_FLOAT_MODE;
import static cn.xfdzx.android.apps.shop.apps.Constants.ONELOGIN_FULLSCREEN_MODE;
import static cn.xfdzx.android.apps.shop.apps.Constants.ONELOGIN_LANDSCAPE_MODE;


/**
 * OneLogin 工具类
 */
public class OneLoginUtils {


    public static final String TAG = "我的";
    private Handler backHandler;
    private Handler mainHandler = new Handler(Looper.getMainLooper());
    private Activity context;
    private Activity ongloginActivity;
    private OneLoginResult oneLoginResult;
    private ProgressDialog progressDialog;
    private SenseBotUtils senseBotUtils;
    private boolean combineWithSenseBot;
    private WeChatLoginInterface weChatLoginInterface;

    /**
     * 一键登录取号时是否使用自定义 loading 对话框
     */
    private boolean useCustomLoading = true;

    public OneLoginUtils(Activity context, OneLoginResult oneLoginResult) {
        this.context = context;
        this.oneLoginResult = oneLoginResult;
        HandlerThread handlerThread = new HandlerThread("oneLogin-demo");
        handlerThread.start();
        backHandler = new Handler(handlerThread.getLooper());
        senseBotUtils = new SenseBotUtils();
    }

    /**
     * 是否开启行为验证
     * @param enable
     */
    public void setSenseBotEnable(boolean enable) {
        this.combineWithSenseBot = enable;
    }

    /**
     * 初始化注册接口
     */
    public static void init(Context context) {
        OneLoginHelper.with()
                .setLogEnable(true)
                .init(context, Constants.APP_ID_OL)
                .register(null, 8000);
    }

    /**
     * 配置页面布局(默认竖屏)
     * 注意事项：
     * 1. 配置项的图片，地址都应为`drawable`目录下。
     * 2. OffsetY与OffsetY_B两者必须有一个值为0,偏移量以不为0的方向作为基准。
     * 3. 颜色为int类型
     *
     * @return config
     */
    private OneLoginThemeConfig initConfig() {
        int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        int screenHeight = context.getResources().getDisplayMetrics().heightPixels;
        int width = DensityUtils.px2dip(context, screenWidth);
        int popWidth = width * 4 / 5;
        int popHeight = DensityUtils.px2dip(context, screenHeight) * 3 / 5;
        int navigationHeight = DensityUtils.px2dip(context, getNavigationBarHeight(context));

        switch (Holder.with().getOneLoginStyle()) {
            case ONELOGIN_FLOAT_MODE:
                return new OneLoginThemeConfig.Builder()
                        .setDialogTheme(true, width, 500, 0, 0, true, true)
                        .setLogBtnLoadingView(useCustomLoading ? "" : "umcsdk_load_dot_white", 20, 20, 12)
                        .setSloganView(0xFFA8A8A8, 10, 370 - navigationHeight, 0, 0)
                        .setAuthBGImgPath("gt_one_login_bg")
                        .setStatusBar(0, 0, false)
                        .setAuthNavLayout(0xFF3973FF, 49, true, false)
                        .setAuthNavTextView("一键登录", 0xFFFFFFFF, 17, false, "服务条款", 0xFF000000, 17)
                        .setAuthNavReturnImgView("gt_one_login_ic_chevron_left_black", 40, 40, false, 8)
                        .setLogoImgView("gt_one_login_logo", 71, 71, false, 125 - navigationHeight, 0, 0)
                        .setNumberView(0xFF3D424C, 24, 200 - navigationHeight, 0, 0)
                        .setSwitchView("切换账号", 0xFF3973FF, 14, false, 249 - navigationHeight, 0, 0)
                        .setLogBtnLayout("gt_one_login_btn_normal", 290, 45, 310 - navigationHeight, 0, 0)
                        .setLogBtnTextView("一键登录", 0xFFFFFFFF, 18)
                        .setPrivacyCheckBox("gt_one_login_unchecked", "gt_one_login_checked", true, 9, 9)
                        .setPrivacyLayout(256, 0, 18, 0, true)
                        .setPrivacyClauseView(0xFFA8A8A8, 0xFF3973FF, 10)
                        .setPrivacyTextView("登录即同意", "和", "、", "并使用本机号码登录")
                        .build();
            case ONELOGIN_DIALOG_MODE:
                return new OneLoginThemeConfig.Builder()
                        .setDialogTheme(true, popWidth, popHeight, 0, 0, false, true)
                        .setLogBtnLoadingView(useCustomLoading ? "" : "umcsdk_load_dot_white", 20, 20, 12)
                        .setLogoImgView("gt_one_login_logo", 71, 71, false, 60, 0, 0)
                        .setAuthNavReturnImgView("gt_one_login_ic_chevron_left_black", 40, 40, false, 8)
                        .setNumberView(0xFF3D424C, 24, 125, 0, 0)
                        .setSwitchView("切换账号", 0xFF3973FF, 14, false, 165, 0, 0)
                        .setLogBtnLayout("gt_one_login_btn_normal", 268, 45, 220, 0, 0)
                        .setLogBtnTextView("一键登录", 0xFFFFFFFF, 18)
                        .setSloganView(0xFFA8A8A8, 10, 270, 0, 0)
                        .setPrivacyLayout(256, 0, 1, 0, true)
                        .build();
            case ONELOGIN_FULLSCREEN_MODE:
                return new OneLoginThemeConfig.Builder()
                        .setStatusBar(0xffffffff, 0xffffffff, true)
                        .setLogoImgView("gt_one_login_logo", 71, 71, true, 60, 0, 0)
                        .setLogBtnLoadingView(useCustomLoading ? "" : "umcsdk_load_dot_white", 20, 20, 12)

                        .setAuthNavReturnImgView("icon_cha",18,18,false,15)
                        .setNumberView(0xFF3D424C, 35, 98, 0, 0)
                        .setSloganView(0xFF989898, 12, 156, 0, 0)
                        .setLogBtnTextView("本机号码一键登录", 0xFFFFFFFF, 16)
                        .setLogBtnLayout("oneclick_button", 326, 46, 208, 0, 0)
                        .setLogBtnTextViewTypeface(Typeface.DEFAULT_BOLD)
                        .setSwitchView("切换账号", 0xFF282828, 16, true, 0, 0, 0)
                        .setPrivacyCheckBox("gt_one_login_unchecked", "gt_one_login_checked", true, 9, 9)
                        .setPrivacyLayout(520, 12, 308, 0, true)
                        .setPrivacyClauseView(0xFFA8A8A8, 0xFF3973FF, 10)
                        .setPrivacyTextView("登录即同意", "和", "、", "并使用本机号码登录")

                        .build();
            case ONELOGIN_LANDSCAPE_MODE:
                return new OneLoginThemeConfig.Builder()
                        .setAuthBGImgPath("gt_one_login_bg")
                        .setDialogTheme(false, 300, 500, 0, 0, false, false)
                        .setStatusBar(0xffffffff, 0xffffffff, true)
                        .setAuthNavLayout(0xFF3973FF, 49, true, false)
                        .setAuthNavTextView("一键登录", 0xFFFFFFFF, 17, false, "服务条款", 0xFF000000, 17)
                        .setAuthNavReturnImgView("gt_one_login_ic_chevron_left_black", 24, 24, false, 12)
                        .setLogoImgView("gt_one_login_logo", 71, 71, false, 55, 0, 0)
                        .setNumberView(0xFF3D424C, 24, 84, 0, 0)
                        .setSwitchView("切换账号", 0xFF3973FF, 14, false, 128, 0, 0)
                        .setLogBtnLayout("gt_one_login_btn_normal", 268, 36, 169, 0, 0)
                        .setLogBtnTextView("一键登录", 0xFFFFFFFF, 15)
                        .setLogBtnLoadingView(useCustomLoading ? "" : "umcsdk_load_dot_white", 20, 20, 12)
                        .setSloganView(0xFFA8A8A8, 10, 226, 0, 0)
                        .setAuthNavReturnImgView("gt_one_login_ic_chevron_left_black", 45, 45, false, 8)
                        .setPrivacyCheckBox("gt_one_login_unchecked", "gt_one_login_checked", true, 9, 9)
                        .setPrivacyLayout(512, 0, 1, 0, true)
                        .setPrivacyClauseView(0xFFA8A8A8, 0xFF3973FF, 10)
                        .setPrivacyTextView("登录即同意", "和", "、", "并使用本机号码登录")
                        .build();
        }
        return new OneLoginThemeConfig.Builder()
                .setLogBtnLoadingView("loading_icon", 20, 20, 12)
                .setAuthNavReturnImgView("gt_one_login_ic_chevron_left_black", 40, 40, false, 8)
                .build();
    }

    /**
     * 自定义控件
     * 注意：横屏的自定义控件的边距需要自己再重新按照设计图设计
     * Demo中自定义控件只支持竖屏，横屏的自定义控件需要自己实现
     */
    private void initView() {
        Button mTitleBtn = new Button(context);
        mTitleBtn.setText("其他");
        mTitleBtn.setTextColor(0xff000000);
        mTitleBtn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        mTitleBtn.setBackgroundColor(Color.TRANSPARENT);
        RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
        mTitleBtn.setLayoutParams(mLayoutParams);


        Button mBtn = new Button(context);
        mBtn.setText("其他方式登录");
        mBtn.setTextColor(0xff3a404c);
        mBtn.setBackgroundColor(Color.TRANSPARENT);
        mBtn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        RelativeLayout.LayoutParams mLayoutParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mLayoutParams1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        mLayoutParams1.setMargins(0, DensityUtils.dip2px(context, 400), 0, 0);
        mBtn.setLayoutParams(mLayoutParams1);


        OneLoginHelper.with().addOneLoginRegisterViewConfig("title_button", new AuthRegisterViewConfig.Builder()
                .setView(mTitleBtn)
                .setRootViewId(AuthRegisterViewConfig.RootViewId.ROOT_VIEW_ID_TITLE_BAR)
                .setCustomInterface(new CustomInterface() {
                    @Override
                    public void onClick(Context context) {
                        Utils.toastMessage(context, "动态注册的其他按钮");
                    }
                })
                .build()
        ).addOneLoginRegisterViewConfig("text_button", new AuthRegisterViewConfig.Builder()
                .setView(mBtn)
                .setRootViewId(AuthRegisterViewConfig.RootViewId.ROOT_VIEW_ID_BODY)
                .setCustomInterface(new CustomInterface() {
                    @Override
                    public void onClick(Context context) {
                        Utils.toastMessage(context, "动态注册的其他登录按钮");
                    }
                })
                .build()
        );
    }

    /**
     * 定义的第三方登录设置
     */
    private void initLoginOld() {
        LayoutInflater inflater1 = LayoutInflater.from(context);
        RelativeLayout relativeLayout = (RelativeLayout) inflater1.inflate(R.layout.layout_other_login, null);
        RelativeLayout.LayoutParams layoutParamsOther = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsOther.setMargins(0, DensityUtils.dip2px(context, 430), 0, 0);
        layoutParamsOther.addRule(RelativeLayout.CENTER_HORIZONTAL);
        relativeLayout.setLayoutParams(layoutParamsOther);
        relativeLayout.findViewById(R.id.weixin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.toastMessage(context, "微信登录");
            }
        });

        OneLoginHelper.with().addOneLoginRegisterViewConfig("title_button", new AuthRegisterViewConfig.Builder()
                .setView(relativeLayout)
                .setRootViewId(AuthRegisterViewConfig.RootViewId.ROOT_VIEW_ID_BODY)
                .setCustomInterface(new CustomInterface() {
                    @Override
                    public void onClick(Context context) {
                        Utils.toastMessage(context, "动态注册的其他按钮");
                    }
                })
                .build()
        );
    }

 /**
     * 定义的第三方登录设置
     */
    private void initLogin() {
        LayoutInflater inflater1 = LayoutInflater.from(context);
        RelativeLayout relativeLayout = (RelativeLayout) inflater1.inflate(R.layout.view_test_one_click_login, null);
        RelativeLayout.LayoutParams layoutParamsOther = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParamsOther.setMargins(0, DensityUtils.dip2px(context, 270), 0, 0);
        layoutParamsOther.addRule(RelativeLayout.CENTER_HORIZONTAL);
        relativeLayout.setLayoutParams(layoutParamsOther);
        relativeLayout.findViewById(R.id.tv_oneclick_mobile).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {

                    onVerifyEnd();

                    context.startActivity(new Intent(context, MobileLoginActivity.class));

                }
            }
        });
        relativeLayout.findViewById(R.id.tv_oneclick_wx_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Utils.toastMessage(context, "微信登录");
                if (Utils.isFastClick()) {
                    EventBus.getDefault().post(new WeChatLoginEvent(1));
                }
            }
        });

        OneLoginHelper.with().addOneLoginRegisterViewConfig("title_button", new AuthRegisterViewConfig.Builder()
                .setView(relativeLayout)
                .setRootViewId(AuthRegisterViewConfig.RootViewId.ROOT_VIEW_ID_BODY)
                .setCustomInterface(new CustomInterface() {
                    @Override
                    public void onClick(Context context) {

                    }
                })
                .build()
        );
    }

    /**
     * 点击直接进行取号
     */
    public void requestToken() {
        oneLoginRequestToken();
    }

    /**
     * 取号接口
     * 在这个方法里也可以配置自定义的布局页面
     * 比如    initView() initLoginOld()
     * 注意:0.8.0之后的版本loading由用户自己控制消失时间
     */
    private void oneLoginRequestToken() {
        if (Holder.with().getOneLoginStyle() == ONELOGIN_FULLSCREEN_MODE) {
            initLogin();
        }
        OneLoginHelper.with().requestToken(initConfig(), oneLoginListener);
    }

    private AbstractOneLoginListener oneLoginListener = new AbstractOneLoginListener() {
        @Override
        public void onResult(final JSONObject jsonObject) {
            Log.i(TAG, "取号结果为：" + jsonObject.toString());
            try {
                int status = jsonObject.getInt("status");
                if (status == Constants.SUCCESS_CODE) {
                    final String process_id = jsonObject.getString("process_id");
                    final String token = jsonObject.getString("token");
                    /**
                     * authcode值只有电信卡才会返回 所以需要判断是否存在 有则进行赋值
                     */
                    final String authCode = jsonObject.optString("authcode");
                    backHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            verify(process_id, token, authCode);
                        }
                    });
                } else {
                    //在页面返回失败的情况下 如果不注重实用返回的监听事件，可以对于返回的事件不进行处理
                    String errorCode = jsonObject.getString("errorCode");
                    if (errorCode.equals("-20301") || errorCode.equals("-20302")) {
                        onVerifyEnd();
                        Log.d(TAG, "当前关闭了授权页面");
                        return;
                    }
                    onVerifyEnd();

                    Log.d(TAG,"取号失败：" + jsonObject.toString());
//                    Utils.toastMessage(context, "取号失败:" + jsonObject.toString());
                    context.startActivity(new Intent(context, MobileLoginActivity.class));

                }
            } catch (JSONException e) {
                onVerifyEnd();

                Log.d(TAG,"取号失败：" + jsonObject.toString());
                context.startActivity(new Intent(context, MobileLoginActivity.class));

//                Utils.toastMessage(context, "取号失败:" + jsonObject.toString());
            }
        }

        @Override
        public void onPrivacyClick(String s, String s1) {
            Log.d(TAG,"当前点击了隐私条款名为：" + s + "---地址为:" + s1);
        }

        @Override
        public void onLoginButtonClick() {
            Log.d(TAG,"当前点击了登录按钮");
        }

        @Override
        public void onAuthActivityCreate(Activity activity) {
            Log.d(TAG,"当前弹起授权页面:" + activity.getClass().getSimpleName());
            if(useCustomLoading) {
                ongloginActivity = activity;
            }

            /**
             * 初始化行为验证 SDK
             */
            if(combineWithSenseBot) {
                senseBotUtils.initSenseBot(activity, onVerifyListener);
            }
        }

        @Override
        public boolean onRequestOtherVerify() {
            /**
             * 启动行为验证 Bind 模式验证
             */
            if(combineWithSenseBot) {
                senseBotUtils.customVerity();
                /**
                 * 返回 true 表示先进行行为验证，验证通过后再进行一键登录取号
                 * 返回 false 时直接进行一键登录取号
                 */
                return true;
            } else {
                return super.onRequestOtherVerify();
            }
        }

        @Override
        public void onAuthWebActivityCreate(Activity activity) {
            Log.d(TAG,"当前弹起授权Web页面:" + activity.getClass().getSimpleName());
        }

        @Override
        public void onPrivacyCheckBoxClick(boolean b) {
            Log.d(TAG,"当前点击了CheckBox---" + b);
        }
    };

    private SenseBotUtils.OnVerifyListener onVerifyListener = new SenseBotUtils.OnVerifyListener() {
        @Override
        public void onSuccess() {
            if (useCustomLoading && ongloginActivity!=null) {
                progressDialog = ProgressDialog.show(ongloginActivity, null, "一键登录取号中", true, true);
            }
            /**
             * 行为验证通过, 继续一键登录取号
             */
            OneLoginHelper.with().requestTokenDelay();
        }

        @Override
        public void onFailure() {

        }
    };

    /**
     * 手机号校验接口 需要网站主配置相关接口进行获取手机号等操作
     * 在这个阶段，也需要调用OneLoginHelper.with().dismissAuthActivity()来进行授权页的销毁
     * 注意:0.8.0之后的版本loading由用户自己控制消失时间
     */
    private void verify(String id, String token, String authCode) {
        JSONObject jsonObject = new JSONObject();
        try {

            if (EmptyUtil.isEmpty(authCode)){
                jsonObject.put("authcode", "");
            }else {
                jsonObject.put("authcode", authCode);
            }
            jsonObject.put("processId", id);
            jsonObject.put("token", token);
            jsonObject.put("clienttype", "1");
            jsonObject.put("type", "2");
            /**
             * 注意，此处的id_2_sign参数是<url>https://onepass.geetest.com/onelogin/result</url>服务端demo接口所需要的参数
             * 具体的参数与网站主服务端有关
             */
//            jsonObject.put("id_2_sign", Constants.APP_ID_OL);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final String result = HttpUtils.requestNetwork(Constants.CHECK_PHONE_URL, jsonObject);
        Log.i(TAG, "校验结果为:" + result);
        mainHandler.post(new Runnable() {
            @Override
            public void run() {
                /**
                 * 关闭loading动画
                 */
                OneLoginHelper.with().stopLoading();

                /**
                 * 关闭授权页面
                 * sdk内部除了返回等相关事件以外是不关闭授权页面的，需要手动进行关闭
                 */

                OneLoginUtils.this.onVerifyEnd();
                try {
                    JSONObject jsonObject1 = new JSONObject(result);
                    int status = jsonObject1.getInt("status");
                    if (status == Constants.SUCCESS_CODE) {

                        APP_TOKEN=jsonObject1.getJSONObject("data").getString("token");
                        UtilsData.getPreference(context,"login").put("token", APP_TOKEN);
                        UtilsData.getPreference(context,"login").put("is_store", EmptyUtil.isEmpty(jsonObject1.getJSONObject("data").getJSONObject("userInfo").getInt("is_store"))?0:jsonObject1.getJSONObject("data").getJSONObject("userInfo").getInt("is_store"));
                        UtilsData.getPreference(context,"login").put("nickname", jsonObject1.getJSONObject("data").getJSONObject("userInfo").getString("nickname"));
                        UtilsData.getPreference(context,"login").put("head_url", jsonObject1.getJSONObject("data").getJSONObject("userInfo").getString("avatar"));
                        EventBus.getDefault().post(new MyInfoRefreshMessage(1));
                        EventBus.getDefault().post(new IMloginMessage(1));//通知im登录

//                        Intent intent = new Intent(context, MainActivity.class);
//                        intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
//                        intent.addFlags(FLAG_ACTIVITY_SINGLE_TOP);
//                        context.startActivity(intent);


                    } else {
                        Utils.toastMessage(context, "校验失败");

                        context.startActivity(new Intent(context, MobileLoginActivity.class));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 一键登录结束
     */
    public void onVerifyEnd() {
        OneLoginHelper.with().dismissAuthActivity();
        oneLoginResult.onResult();
        if (useCustomLoading) {
            if (progressDialog != null) {
                progressDialog.dismiss();
                progressDialog = null;
            }
            ongloginActivity = null;
        }
        if(senseBotUtils!=null) {
            senseBotUtils.onDestroy();
        }
    }

    /**
     * 判断是否有NavigationBar
     *
     * @param activity
     * @return
     */
    public static boolean checkHasNavigationBar(Activity activity) {
        WindowManager windowManager = activity.getWindowManager();
        Display d = windowManager.getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            d.getRealMetrics(realDisplayMetrics);
        }

        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        d.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

        return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    }

    /**
     * 获得NavigationBar的高度
     */
    public static int getNavigationBarHeight(Activity activity) {
        int result = 0;
        Resources resources = activity.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0 && checkHasNavigationBar(activity)) {
            result = resources.getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
